
package com.android.finaltracer.model;

import org.json.JSONException;
import org.json.JSONObject;

import com.android.finaltracer.channel.Channel;

/*
 * 数据的解压缩工具类
 * 
 * */
public abstract class DataFormatBasic implements DataFormatInterface {
    // Data Type define
    public static final int TYPE_NOTHING = 0;
    public static final int TYPE_POSITION_INFO = TYPE_NOTHING + 1;
    public static final int TYPE_COMMAND_INFO = TYPE_POSITION_INFO + 1;
    public static final int TYPE_BIND_INFO = TYPE_COMMAND_INFO + 1;
    public static final int TYPE_SYNC_INFO = TYPE_BIND_INFO + 1;
    public static final int TYPE_INTERNAL_TIMER_OUT = TYPE_SYNC_INFO + 1;
    public static final int TYPE_INTERNAL_SYNC_RESULT = TYPE_INTERNAL_TIMER_OUT + 1;

    // JSON tags define
    public static final String TAG_TYPE = "type";
    public static final String TAG_BODY = "body";

    // public DataFormatBasic() {
    // super();
    // }

    public String makeHeader() {
        String header = "";
        try {
            JSONObject json = new JSONObject();
            json.put(Channel.FINALTRACER_TYPE, Channel.TYPE_SMS).put(TAG_TYPE,
                    getType());
            header += json.toString();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return header;
    }

    public int getTypeFromString(String str) {
        int type = TYPE_NOTHING;
        try {
            JSONObject json = new JSONObject(str);
            int t = json.getInt(TAG_TYPE);
            if (t != TYPE_NOTHING) {
                type = t;
            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return type;
    }
}
